package com.etcn.common.schedule;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.etcn.modules.busi.member.pojo.WmMember;
import com.etcn.modules.busi.member.service.IWmMemberService;

import ch.qos.logback.classic.Logger;

/**
 * 检查vip是否过期
 * @author guozc
 */
@Component
public class MemberVipCheckSchedule {
    private Logger serviceLogger = (Logger) LoggerFactory.getLogger("service_file_log");
	
	@Autowired
	@Qualifier("busiJdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IWmMemberService wmMemberService;

	/**
	 * 执行方法
	 * 5分钟检查一次
	 */
	@Scheduled(cron = "0 0/5 * * * ?")
    public void run() throws Exception {
		serviceLogger.info("---------------开始检查会员vip是否失效--------------");
		String sql = "select * From wm_member where vip_end_time is not null and  NOW() > vip_end_time";
		List<WmMember> invalidVips = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(WmMember.class));
		for(WmMember member : invalidVips){
			member.setVipProductId("0");// 设置成免费套餐
			member.setVipFlag("0");
			member.setVipEndTime(null);
			wmMemberService.vipExpireUpdate(member);
			serviceLogger.info("更新会员：" + member.getMobile() + " 为普通会员");
		}
    }

}
